package com.labyrinth.core;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class LabyrinthEscapeCore implements ApplicationListener
{
    private boolean keyPressed = false;
    private boolean hasPosition = false;
    private Vector2D originPosition;
    private int LevelNr = 1;
    private int MaxLevel = 3;
    private SpriteBatch batch;
    private final Level level = new Level();
    private GameObject[][] brickLevel;
    private GameInterface map;

    @Override
    public void create()
    {
        batch = new SpriteBatch();
        map = new GameInterface(300, 760);
        level.ReadLevel(LevelNr);
        map.setStartMove(level.getMoves());

        brickLevel = new GameObject[level.getRow()][level.getCol()];
        brickLevel = level.getBrickLevel();
        map.setMaxLvL(level.maxLevels);
        MaxLevel= level.maxLevels;
        map.setStartT(level.getTime());
        map.Counter(map.moveCount);

        Gdx.gl10.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        Gdx.gl10.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);
        Gdx.gl10.glLoadIdentity();
        Gdx.glu.gluOrtho2D(Gdx.gl10, 0, Gdx.graphics.getWidth(), 0, Gdx.graphics.getHeight());
    }

    @Override
    public void dispose()
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause()
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void render()
    {

        Gdx.gl10.glClear(GL10.GL_COLOR_BUFFER_BIT);
        Gdx.gl10.glMatrixMode(GL10.GL_MODELVIEW);
        Gdx.gl10.glLoadIdentity();
        
      batch.begin();
        if(!map.Menu)
        {

            for (int i = level.getRow() - 1; i >= 0; i--)
            {
                for (int u = 0; u < level.getCol(); u++)
                {
                    if (brickLevel[i][u] != null)
                    {
                        brickLevel[i][u].draw();
                    }
                }
            }

            level.player.draw();
            
            if(map.WinnerScreen && !map.Menu)
            {
            	 map.WinnerScreen = false;
            }
             map.draw();
        }
        else
        {
            map.draw();
        }

        batch.end();



        if (Gdx.input.isTouched())
        {
            if (!hasPosition)
            {
                originPosition = new Vector2D(Gdx.input.getX(), (Gdx.graphics.getHeight()) - Gdx.input.getY());
                hasPosition = true;
            }
            
            keyPressed = true;
        }
        else if (keyPressed)
        {

            for (int i = level.getRow() - 1; i >= 0; i--)
            {
                for (int u = 0; u < level.getCol(); u++)
                {
                    if (brickLevel[i][u].getClass().equals(MovableBrick.class))
                    {
                        MovableBrick brick = (MovableBrick) brickLevel[i][u];

                        if ((brick.getDirection() != -1 && brick.checkInnerCircle(originPosition))&&(map.moveCount != 0))
                        {
                            if(brick.moveBrick(originPosition, new Vector2D(Gdx.input.getX(), (Gdx.graphics.getHeight()) - Gdx.input.getY()), brickLevel, i, u,level.player))
                            {
                                if(!map.infiniteMove)
                                {
                                map.moveCount--;
                                map.Counter(map.moveCount);
                                }
                                break;
                            }
                        }
                    }
                }
            }
           if (originPosition != null && (Math.pow(map.position.getX() - originPosition.getX(), 2) + Math.pow(map.position.getY() - originPosition.getY(), 2)) <= Math.pow(20, 2))
            {
                RESET();
            }
            if(!(map.MenuB.clicked(Gdx.input.getX(), (Gdx.graphics.getHeight()) - Gdx.input.getY())))
            {
                map.Menu = true;
                map.Screen = "Menu";
            }

            if(map.Menu)
            {
                map.Clicked(Gdx.input.getX(),(Gdx.graphics.getHeight()) - Gdx.input.getY());
                if(!map.Menu)
                {
                    LevelNr = map.mazeNR; 
                    ChangeLevel(map.mazeNR);
                }
            }

            keyPressed = false;
            hasPosition = false;
        }

        level.player.collisionDetection(brickLevel);
        if(!level.player.Moved && !map.WinnerScreen)
        {
        level.player.Moved = true;
        }
        if(level.player.gameConditionCheck(brickLevel, Trap.class))
        {
            RESET();
        }
        else if(level.player.gameConditionCheck(brickLevel, Enemy.class))
        {
            RESET();
        }
        else if(level.player.gameConditionCheck(brickLevel, Exit.class))
        {


            if(LevelNr == MaxLevel)
            {
                LevelNr = 1;
                map.mazeNR = 1;
            }
            else
            {
                LevelNr++;
                map.mazeNR++;
            }

            map.WinnerScreen = true;
            map.Menu = true;
            map.Screen = "Winner";
            RESET();
           
        }
        if(map.start == 0 && map.startT != 0)
        {
            RESET();
        }

        if((level.player.Moved) && (!map.Menu) && !map.LevelStart)
        {
                map.LevelStart = true;
        }

    }

    void RESET()
    {
        level.changeLevel();
        map.start = 1;
        map.last = 1;
        map.bleh = 1;
        map.moveCount = 0;

        map.setStartMove(level.getMoves());
        map.setStartT(level.getTime());
        map.Counter(map.moveCount);
        level.player.Moved = false;
        map.LevelStart = false;
    }

    void ChangeLevel(int levelChoice)
    {
        level.ReadLevel(levelChoice);
        brickLevel = level.getBrickLevel();
        map.mazeNR = levelChoice;

        RESET();
    }


    @Override
    public void resize(int arg0, int arg1)
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume()
    {
        // TODO Auto-generated method stub

    }
    

}
